package cn.tangwei.product.service.impl;

import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.org.domain.Employee;
import cn.tangwei.org.mapper.EmployeeMapper;
import cn.tangwei.product.domain.Product;
import cn.tangwei.product.domain.ProductDetail;
import cn.tangwei.product.mapper.ProductDetailMapper;
import cn.tangwei.product.mapper.ProductMapper;
import cn.tangwei.product.service.IProductService;
import cn.tangwei.user.domain.LoginInfo;
import cn.tangwei.user.mapper.LoginInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void onsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaleTime", new Date());
        productMapper.onsale(map);
    }

    @Override
    public void offsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaleTime", new Date());
        productMapper.offsale(map);
    }

    @Override
    public void add(Product product, LoginInfo loginInfo) {
        Long id = loginInfo.getId();
        Employee employee = employeeMapper.loadByLoginInfoId(id );
        Long shop_id = employee.getShop_id();
        product.setShop_id(shop_id);
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        if(detail != null){
            detail.setProduct_id(product.getId());
            productDetailMapper.save(detail);
        }
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        productDetailMapper.update(detail);
    }
}
